package cn.tedu.springbootdemo.Impl;

import cn.tedu.springbootdemo.dao.UserDao;
import cn.tedu.springbootdemo.entity.User;
import cn.tedu.springbootdemo.exception.DeleteFailedException;
import cn.tedu.springbootdemo.exception.IllegalParameterException;
import cn.tedu.springbootdemo.exception.PasswordErrorException;
import cn.tedu.springbootdemo.exception.RegistrationFailedException;
import cn.tedu.springbootdemo.exception.UpdateFailedException;
import cn.tedu.springbootdemo.exception.UserNotFoundException;
import cn.tedu.springbootdemo.exception.UsernameExistsException;
import cn.tedu.springbootdemo.service.UserService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
//@Transactional
public class UserServiceImpl implements UserService {

    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    private UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    //@Transactional(timeout = 30)//事务所指定的超时时间单位s
    //@Transactional(timeout = 30)//(timeout = -1)指定事务永不超时.事务所指定的超时时间单位s
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED) //默认就是永不超时
    public User regist(String username, String password) {
        if (username==null || password==null) {
            throw new IllegalParameterException("参数异常！");
        }

        User findUser = userDao.findUserByName(username);
        if (findUser!=null) {
            logger.warn("用户名 {} 已经被注册！", username);
            throw new UsernameExistsException("该用户名已经被注册！");
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int i = userDao.addUser(user);
        logger.debug("更新数量 {}", i);
        if (i!=1) {
            logger.warn("注册用户失败！");
            throw new RegistrationFailedException("注册用户失败！");
        }
        return user;
    }
    // 如何全面测试Login方法？
    // 登录成功, 登录失败， 各种异常！
    @Override
    //@Transactional
    public User login(String username, String password) {
        if (username == null || password == null){
            logger.warn("用户名或者密码为null");
            throw new IllegalParameterException("用户名密码不能为空！");
        }
        if (username.trim().isEmpty() || password.trim().isEmpty()) {
            logger.warn("用户名或者密码为null");
            throw new IllegalParameterException("用户名密码不能为空！");
        }
        username = username.trim();
        User user = userDao.findUserByName(username);

        if (user==null) {
            logger.warn("用户 {} 不存在！", username);
            throw new UserNotFoundException("该用户不存在！");
        }
        if (!user.getPassword().equals(password)) {
            logger.warn("密码错误！{} ", password);
            throw new PasswordErrorException("密码错误！");
        }
        return user;
    }

    @Override
    public List<User> list() {
        return userDao.findAllUser();
    }

    @Override
    public User getById(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    public User getByUsername(String username) {
        return userDao.findUserByName(username);
    }

    @Override
    //@Transactional //Spring提供的事务管理
    public void update(User user) {
        int n = userDao.updateUser(user);
        if (n != 1){
            throw new UpdateFailedException("更新失败！");
        }
    }

    @Override
    public User delete(Integer id) {
        User user = userDao.findUserById(id);
        if (user == null){
            throw new UserNotFoundException("没有用户id");
        }
        int n = userDao.deleteUser(id);
        if (n != 1){
            throw new DeleteFailedException("更新失败！");
        }
        return user;
    }

    @Override
    public void addUser(User user) {
        userDao.addUser(user);
    }
}
